package cn.edu.seu.herald.informationplatform.dao.database;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Collection;
import java.util.LinkedList;

import cn.edu.seu.herald.informationplatform.bean.league.Vote;
import cn.edu.seu.herald.informationplatform.bean.league.VoteItem;

public class VoteItemDataAccess extends DataAccessObject {
	
	private static final String COLUMN_ID = "id";
	
	private static final String COLUMN_VOTE_ID = "vote_id";
	
	private static final String COLUMN_NAME = "name";
	
	private static final String COLUMN_CONTENT = "content";
	
	private static final String COLUMN_PICTURE = "picture";
	
	private static final String COLUMN_COUNT = "count";
	
	private static VoteItemDataAccess _instance;
	
	private VoteDataAccess _voteDao;
	
	public static VoteItemDataAccess getInstance() {
		if (_instance == null) {
			_instance = new VoteItemDataAccess();
		}
		return _instance;
	}
	
	protected VoteItemDataAccess() {
		_voteDao = VoteDataAccess.getInstance();
	}
	
	private VoteItem getVoteItemByResultSet(ResultSet rs)
			throws SQLException {
		int voteItemId = rs.getInt(COLUMN_ID);
		int voteId = rs.getInt(COLUMN_VOTE_ID);
		String name = rs.getString(COLUMN_NAME);
		String content = rs.getString(COLUMN_CONTENT);
		String picture = rs.getString(COLUMN_PICTURE);
		int count = rs.getInt(COLUMN_COUNT);
		
		Vote vote = _voteDao.getVoteById(voteId);
		VoteItem voteItem = new VoteItem();
		voteItem.setName(name);
		voteItem.setContent(content);
		voteItem.setId(voteItemId);
		voteItem.setPicture(picture);
		voteItem.setVote(vote);
		voteItem.setCount(count);
		
		return voteItem;
	}
	
	private static final String STATEMENT_RETRIEVE_VOTE_ITEM_BY_ID =
			"SELECT * FROM lg_vote_item WHERE id=?;";
	
	public VoteItem getVoteItemById(int id) throws SQLException {
		Connection connection = getConnectionLeague();
		try {
			PreparedStatement ps =
					connection.prepareStatement(STATEMENT_RETRIEVE_VOTE_ITEM_BY_ID);
			ps.setInt(1, id);
			ResultSet rs = ps.executeQuery();
			
			if (!rs.next()) {
				return null;
			}
			
			return getVoteItemByResultSet(rs);
		} finally {
			connection.close();
		}
	}
	
	private static final String STATEMENT_RETRIEVE_VOTE_ITEMS_BY_VOTE_ID =
			"SELECT * FROM lg_vote_item WHERE vote_id=?;";
	
	public Collection<VoteItem> getVoteItemsByVoteId(int voteId)
			throws SQLException {
		Connection connection = getConnectionLeague();
		try {
			PreparedStatement ps = connection.prepareStatement(
					STATEMENT_RETRIEVE_VOTE_ITEMS_BY_VOTE_ID);
			ps.setInt(1, voteId);
			ResultSet rs = ps.executeQuery();
			
			LinkedList<VoteItem> voteItems = new LinkedList<VoteItem>();
			while (rs.next()) {
				voteItems.add(getVoteItemByResultSet(rs));
			}
			
			return voteItems;
		} finally {
			connection.close();
		}
	}
	
	private static final String STATEMENT_CREATE_VOTE_ITEM =
			"INSERT INTO lg_vote_item (vote_id, name, content, picture) VALUES " +
			"(?, ?, ?,?);";
	
	public int createNewVoteItem(VoteItem voteItem) throws SQLException {
		Connection connection = getConnectionLeague();
		try {
			PreparedStatement ps = connection.prepareStatement(
					STATEMENT_CREATE_VOTE_ITEM, Statement.RETURN_GENERATED_KEYS);
			
			ps.setInt(1, voteItem.getVote().getId());
			ps.setString(2, voteItem.getName());
			ps.setString(3, voteItem.getContent());
			ps.setString(4, voteItem.getPicture());
			ps.executeUpdate();
			ResultSet rs = ps.getGeneratedKeys();
			
			if (!rs.next()) {
				return -1;
			}
			return rs.getInt(1);
		} finally {
			connection.close();
		}
	}
	
	private static final String STATEMENT_UPDATE_VOTE_ITEM =
			"UPDATE lg_vote_item SET name=?, content=?, picture=? WHERE id=?;";
	
	public void updateVoteItem(VoteItem voteItem) throws SQLException {
		Connection connection = getConnectionLeague();
		try {
			PreparedStatement ps = connection.prepareStatement(
					STATEMENT_UPDATE_VOTE_ITEM);
			ps.setString(1, voteItem.getName());
			ps.setString(2, voteItem.getContent());
			ps.setString(3, voteItem.getPicture());
			ps.setInt(4, voteItem.getId());
			ps.executeUpdate();
		} finally {
			connection.close();
		}
	}
	
	private static final String STATEMENT_DELETE_VOTE_ITEM_BY_ID =
			"DELETE FROM lg_vote_item WHERE id=?;";
	
	public void deleteVoteItemById(int id) throws SQLException {
		Connection connection = getConnectionLeague();
		try {
			PreparedStatement ps = connection.prepareStatement(
					STATEMENT_DELETE_VOTE_ITEM_BY_ID);
			ps.setInt(1, id);
			ps.executeUpdate();
		} finally {
			connection.close();
		}
	}
	
	private static final String STATEMENT_DELETE_VOTE_ITEMS_BY_VOTE_ID =
			"DELETE FROM lg_vote_item WHERE vote_id=?;";
	
	public void deleteVoteItemsByVoteId(int voteId) throws SQLException {
		Connection connection = getConnectionLeague();
		try {
			PreparedStatement ps = connection.prepareStatement(
					STATEMENT_DELETE_VOTE_ITEMS_BY_VOTE_ID);
			ps.setInt(1, voteId);
			ps.executeUpdate();
		} finally {
			connection.close();
		}
	}
	

}
